package com.afkt.project.ui.slice;


import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.common.DevFinal;
import com.dev.base.widget.BaseImageView;
import com.google.zxing.Result;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseButton;
import com.dev.base.widget.BaseTextView;
import com.dev.other.ZXingQRCodeUtils;
import com.dev.utils.app.HandlerUtils;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.TextViewUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import com.dev.utils.common.StringUtils;
import com.dev.utils.common.ThrowableUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.media.image.PixelMap;


public class QRCodeImageSlice extends BaseActivity implements Component.ClickedListener {

    private BaseTextView vidAqiTv;

    private BaseImageView vidAqiSelectBtn;

    private PixelMap selectBitmap;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;

    private static final String TAG = QRCodeImageSlice.class.getSimpleName();

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_qrcode_image;
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("二维码图片解析");
        bar_Layout.addComponent(bar);

        vidAqiTv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aqi_tv);
        vidAqiSelectBtn = (BaseImageView) findComponentById(ResourceTable.Id_vid_aqi_select_btn);


        ZXingQRCodeUtils.decodeQRCode(vidAqiSelectBtn.getPixelMap(), (success, result, e) -> HandlerUtils.postRunnable(new Runnable() {
            @Override
            public void run() {
                if (success) {
                    StringBuilder builder = new StringBuilder();
                    builder.append("二维码解析数据: \n");

                    builder.append(StringUtils.checkValue(DevFinal.NULL_STR, ZXingQRCodeUtils.getResultData(result)));
                    TextViewUtils.setText(vidAqiTv, builder.toString());
                } else {
                    TextViewUtils.setText(vidAqiTv, "图片非二维码 / 识别失败\n" + ThrowableUtils.getThrowableStackTrace(e));
                }
            }
        }));
    }


    public void initListener() {
        //
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }


    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_vid_aqi_tv:
                String text = TextViewUtils.getText(vidAqiTv);
                if (text == null || text.length() == 0) return;

                ToastTintUtils.success(ResourceUtils.getString(ResourceTable.String_str_copy_suc) +  " -> " + text);
                break;
        }
    }
}
